package com.koron.css2.newProjectCenter.impl;

import com.koron.css2.ImportExcelInterface;
import com.koron.css2.baseConfig.mapper.MeterBoreMapper;
import com.koron.css2.baseConfig.mapper.WaterTypeMapper;
import com.koron.css2.baseConfig.queryBean.MeterBoreQueryBean;
import com.koron.css2.baseConfig.queryBean.WaterTypeQueryBean;
import com.koron.css2.baseConfig.vo.MeterBoreVO;
import com.koron.css2.baseConfig.vo.WaterTypeVO;
import com.koron.css2.clientCenter.bean.UserBean;
import com.koron.css2.common.dto.HeaderInfoDto;
import com.koron.css2.meterService.dto.MsInfoDto;
import com.koron.css2.newProjectCenter.bean.PjMeter;
import com.koron.css2.newProjectCenter.bean.query.PjMeterQuery;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.web.multipart.MultipartFile;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 工程报装-工程登记节点水表信息导入
 */
public class PjMeterRegisterImport implements ImportExcelInterface {

    @Override
    public MessageBean<?> importExel(SessionFactory factory, UserInfoBean userInfo, RequestBean req, List<Map<String, String>> data, MultipartFile file) {
        try {
            WaterTypeMapper waterTypeMapper = factory.getMapper(WaterTypeMapper.class);
            MeterBoreMapper meterBoreMapper = factory.getMapper(MeterBoreMapper.class);
            List<PjMeter> pjMeterList = new ArrayList<>();
            // 查询处所有的用水类型
            List<WaterTypeVO> waterTypeList = waterTypeMapper.findWaterType(new WaterTypeQueryBean());
            // 查询出所有的水表口径
            List<MeterBoreVO>  meterBoreList = meterBoreMapper.selectMeterBoreList(new MeterBoreQueryBean());

            PjMeterQuery pjMeterQuery = JsonUtils.objectToPojo(req.getData(), PjMeterQuery.class);

            // 检验excel里的户号是否合法
            PjMeterInstallBuildImport.checkImportUserNo(factory, data, pjMeterQuery, userInfo, true);

            for (int i = 0; i < data.size(); i++) {
                Map<String, String> map = data.get(i);
                UserBean userBean = new UserBean();
                PjMeter pjMeter = new PjMeter();
                MsInfoDto msInfo = new MsInfoDto();

                // 用水类型
                String useWaterTypeName = map.get("useWaterTypeName");
                if (StringUtils.isNotBlank(useWaterTypeName)) {
                    // 通过用水类型名称找出具体的用水类型的信息
                    WaterTypeVO waterType = waterTypeList.stream().filter(y -> y.getWaterTypeName().equals(useWaterTypeName)).findFirst().orElse(new WaterTypeVO());
                    if (waterType.getWaterTypeName() == null){
                        return MessageBean.create(Constant.MESSAGE_INT_FAIL, "第" + (i + 1) + "行请输入正确的用水类型名称", null);
                    }
                    userBean.setUseWaterType(waterType.getId());
                    userBean.setUseWaterTypeName(waterType.getWaterTypeName());
                }

                // 装表地址
                userBean.setSetupMeterAddr(Objects.toString(map.get("setupMeterAddr"), ""));
                // 用户编号
                userBean.setUserNo(Objects.toString(map.get("userNo"), ""));
                // 用户ID
                userBean.setId(map.get("userInfoTempId"));
                // 水表口径
                String meterBoreName = map.get("meterBoreName");
                if (StringUtils.isNotBlank(meterBoreName)) {
                    // 通过水表口径名称找出具体的用水类型的信息
                    MeterBoreVO meterBore = meterBoreList.stream().filter(y -> y.getBoreName().equals(meterBoreName)).findFirst().orElse(new MeterBoreVO());
                    if (meterBore.getBoreName() == null){
                        return MessageBean.create(Constant.MESSAGE_INT_FAIL, "第" + (i + 1) + "行请输入正确的水表口径", null);
                    }
                    msInfo.setMeterBore(meterBore.getId());
                    msInfo.setMeterBoreName(meterBoreName);
                }

                pjMeter.setUserInfo(userBean);
                pjMeter.setUserInfoTempId(userBean.getId());
                pjMeter.setMsInfo(msInfo);
                pjMeterList.add(pjMeter);
            }
            MessageBean<List> msg = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "水表信息导入成功", List.class);
            msg.setData(pjMeterList);
            return msg;
        } catch (Exception e) {
            logger.error("工程报装-工程登记-水表信息导入失败", e);
        }
        return MessageBean.create(Constant.MESSAGE_INT_FAIL, "水表信息导入失败", null);
    }

    @Override
    public List<HeaderInfoDto> getHeaderInfo(UserInfoBean userInfo) {
        List<HeaderInfoDto> headerInfos = new ArrayList<HeaderInfoDto>();
        headerInfos.add(new HeaderInfoDto("用户编号", false, "userNo"));
        headerInfos.add(new HeaderInfoDto("装表地址", false, "setupMeterAddr"));
        headerInfos.add(new HeaderInfoDto("用水类型", false, "useWaterTypeName"));
        headerInfos.add(new HeaderInfoDto("水表口径", false, "meterBoreName"));
        return headerInfos;
    }

    @Override
    public boolean isCheckHeader() {
        return true;
    }
}
